<!DOCTYPE html>
<html lang="zh">

<head>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>页面</title>
    <script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/loading.js"></script>
    <link href="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/theme-chalk/index.css" rel="stylesheet">
</head>
<style>
    .spacing {
        height: 50px;
    }
</style>
<body>
<div id="app">
    <el-container v-loading="loading">
        <el-main>
        <el-header>
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">订单管理</el-breadcrumb-item>
                <el-breadcrumb-item>订单列表</el-breadcrumb-item>
            </el-breadcrumb>
        </el-header>
            <el-row :gutter="20" style="margin-left: 0;margin-right: 0">
                <el-col :xs="8" :sm="8" :md="4" :lg="3" :xl="3" class="spacing">
                    <el-input placeholder="请输入订单号" v-model="sales_order_no">
                    </el-input>
                </el-col>
                <el-col :xs="8" :sm="8" :md="4" :lg="3" :xl="3" class="spacing">
                    <el-input placeholder="请输入下单员" v-model="sales_man">
                    </el-input>
                </el-col>
                <el-col :xs="8" :sm="8" :md="4" :lg="3" :xl="3" class="spacing">
                    <el-date-picker
                            v-model="startTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="开始日期" style="width: 100%">
                    </el-date-picker>
                </el-col>
                <el-col :xs="8" :sm="8" :md="4" :lg="3" :xl="3" class="spacing">
                    <el-date-picker
                            v-model="endTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="结束日期" style="width: 100%">
                    </el-date-picker>
                </el-col>
                <el-col :xs="8" :sm="8" :md="4" :lg="3" :xl="3" class="spacing">
                    <el-input placeholder="请输入供应商联系人" v-model="buyer">
                    </el-input>
                </el-col>
                <el-col :xs="8" :sm="8" :md="4" :lg="3" :xl="3" class="spacing">
                    <el-select v-model="sales_status" placeholder="请选择订单状态" style="width: 100%">
                        <el-option v-for="item in sales_status_list" :key="item.sales_status_code"
                                   :label="item.sales_status"
                                   :value="item.sales_status_code">
                        </el-option>
                    </el-select>
                </el-col>
                <el-col :xs="8" :sm="8" :md="4" :lg="3" :xl="3" class="spacing">
                    <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
                </el-col>
                <el-col :xs="8" :sm="8" :md="4" :lg="3" :xl="3" class="spacing">
                     <#if bool>
                             <a href="/order/addOrder.page?admin=false">
                                 <el-button type="primary" icon="el-icon-plus">新增合同订单</el-button>
                             </a>
                     </#if>
                </el-col>
            </el-row>
            <el-table v-loading="tableLoading" :data="tableData" stripe border style="width:100%" highlight-current-row
                      size="mini" tooltip-effect="dark" height="calc(100vh - 240px)">
                　　
                <el-table-column label="序号" align="center" width="50px">
                    　　
                    <template scope="scope"><span>{{scope.$index+(currentPage - 1) * pageSize + 1}}</span></template>
                </el-table-column>
                <el-table-column prop="orderBill_id" label="订单编号" align="center" :show-overflow-tooltip=true>
                    　　
                </el-table-column>
                　　
            <#--<el-table-column prop="order_name" label="订单名称" align="center">-->
            <#--　　-->
            <#--</el-table-column>-->
                <el-table-column prop="user_dept" label="部门" align="center" :show-overflow-tooltip=true>
                    　　
                </el-table-column>
                <el-table-column prop="user_name" label="下单员" align="center" :show-overflow-tooltip=true>
                    　　
                </el-table-column>
                　　
                <el-table-column prop="userGroup_name" label="客户名称" align="center" :show-overflow-tooltip=true>
                    　　
                </el-table-column>
                <el-table-column prop="buyer" label="供应商联系人" align="center" :show-overflow-tooltip=true>
                    　　
                </el-table-column>
                <el-table-column prop="crtTime" label="下单时间" align="center" :show-overflow-tooltip=true>
                </el-table-column>
                <el-table-column v-if prop="price" label="订单金额（元）" align="center" :show-overflow-tooltip=true>
                </el-table-column>
                <el-table-column prop="orderBill_customStat" label="订单状态" align="center" :formatter="formatStatus"
                                  :show-overflow-tooltip=true>
                </el-table-column>
                <el-table-column label="操作" align="center" min-width="140" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text"
                                   @click="exportOrder(scope.row.orderBill_id,scope.row.orderBean_pk)">导出
                        </el-button>
                    <#--<el-button type="text"-->
                    <#--@click="printOrder(scope.row.orderBill_id,scope.row.orderBean_pk)">打印-->
                    <#--</el-button>-->
                    <#--<a href="/order/printOrder.page?orderNo=scope.row.orderBill_id&orderBean_pk=scope.row.orderBean_pk"-->
                    <#--target="_blank">打印-->
                    <#--</a>-->
                    <#--等待甲方方确认状态-->
                        <el-button type="text" v-if="scope.row.orderBill_customStat==1000"
                                   @click="affirmOrder(scope.row.orderBill_id,'del','1003',scope.row.orderBean_pk)">删除
                        </el-button>
                        <el-button type="text" v-if="scope.row.orderBill_customStat==1000"
                                   @click="detail(scope.row.orderBill_id,scope.row.orderBean_pk)">查看
                        </el-button>
                    <#--等待甲方确认状态-->
                        <el-button type="text" v-if="scope.row.orderBill_customStat==1001"
                                   @click="affirmOrder(scope.row.orderBill_id,'del','1003',scope.row.orderBean_pk)">删除
                        </el-button>
                        <el-button type="text" v-if="scope.row.orderBill_customStat==1001"
                                   @click="affirmOrder(scope.row.orderBill_id,'affirm','1002',scope.row.orderBean_pk)">
                            确认
                        </el-button>
                        <el-button type="text" v-if="scope.row.orderBill_customStat==1001"
                                   @click="detail(scope.row.orderBill_id,scope.row.orderBean_pk)">查看
                        </el-button>
                    <#--甲乙放同时确认完成-->
                        <el-button type="text" v-if="scope.row.orderBill_customStat==1002"
                                   @click="modify(scope.row.orderBill_id,scope.row.price,scope.row.orderBean_pk,scope.row.payment)">
                            付款
                        </el-button>
                        <el-button type="text" v-if="scope.row.orderBill_customStat==1002"
                                   @click="detail(scope.row.orderBill_id,scope.row.orderBean_pk)">查看
                        </el-button>
                        <el-button type="text" v-if="scope.row.orderBill_customStat==1004"
                                   @click="detail(scope.row.orderBill_id,scope.row.orderBean_pk)">查看
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-main>
        <el-footer>
            <el-row>
                <el-col>
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                                   :page-sizes="[100, 500, 1000]" :page-size="pageSize"
                                   layout="total, sizes, prev, pager, next, jumper"
                                   :total="totalRecords">
                    </el-pagination>
                </el-col>
            </el-row>
        </el-footer>
        <el-dialog title="请上传凭证" :visible.sync="dialogFormVisible" ref="upload" width="60%">
            <el-form label-width="30%">
                <el-row>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                        <el-form-item label="订单编号" prop="sales_order_no">
                            <el-input :disabled="true" v-model="sales_order_no"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                        <el-form-item label="付款金额">
                            <el-input v-model="sales_amount">
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                        <el-form-item label="凭证上传">
                            <el-upload class="upload-demo" :on-success="getUrl" action="/order/file.json"
                                       accept=".jpeg,.png,.gif" :before-upload="beforeAvatarUpload"
                                       :on-remove="handleRemove" :on-exceed="onExceed" multiple :limit="1"
                                       list-type="picture-card">
                                <el-button size="small" type="primary">点击上传</el-button>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitUpload">确 定</el-button>
            </div>
        </el-dialog>
    </el-container>
</div>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/dist/vue.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/index.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/publiccss/assets/global/plugins/jquery.min.js"
        type="text/javascript"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                loading:false,
                tableLoading: false,
                dialogFormVisible: false,
                dialogVisible: false,
                dialogImageUrl: '',
                sales_order_no: '',
                sales_order_name: '',
                buyer: '',
                sales_amount: 0,
                tableData: [],
                currentPage: 1, //当前页
                pageSize: 100, //每页总数
                totalRecords: 0, //总条数
                sales_status_list: [{
                    sales_status_code: '',
                    sales_status: '请选择'
                }, {
                    sales_status_code: 1000,
                    sales_status: '已保存（待供方确认）'
                }, {
                    sales_status_code: 1001,
                    sales_status: '下单中（待需方确认）'
                }, {
                    sales_status_code: 1002,
                    sales_status: '付款中'
                }, {
                    sales_status_code: 1004,
                    sales_status: '已结单'
                }],
                sales_status: "",
                time_zones: "",
                sales_order_no: '',
                sales_man: "",
                file: '',
                param: '',
                startTime: '',
                endTime: '',
                totalPrice: 0,
                payPrice: 0,
                orderBean_pk: ''
            };
            show: false

        },

        methods: {
            exportOrder(orderNo, orderBean_pk) {
                window.location.href = "/order/export.json?orderNo=" + orderNo + "&orderBean_pk=" + orderBean_pk;
            },
            printOrder(orderNo, orderBean_pk) {
                window.open("/order/printOrder.page?orderNo=" + orderNo + "&orderBean_pk=" + orderBean_pk)
            },
            onExceed(files, fileList) {
                this.$message({
                    type: 'info',
                    message: '最多只能上传一个图片',
                    duration: 6000
                });
            },
            beforeAvatarUpload(file) {
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isLt2M) {
                    this.$message.error('图片大小不能超过 2MB!');
                }
                return isLt2M;
            },


            getUrl(file) {
                this.file = file;
            },
            handleRemove(file) {
                var bool = false;
                var err = "系统异常！";
                $.ajax({
                    url: '/order/delFile.json',
                    type: 'post',
                    data: {path: file.response},
                    async: false,
                    dataType: 'json',
                    success: function (result) {
                        if (result.ret) {
                            bool = true;
                        } else {
                            err = result.msg;
                        }
                    }
                });
                if (bool) {
                    this.$message({
                        message: "操作成功",
                        type: 'success'
                    });
                } else {
                    this.$message({
                        message: err,
                        type: 'error'
                    });
                }
            },

            submitUpload() {
                var bool = false;
                var err = "系统异常！";
                var file = this.file;
                var sales_amount = Number(this.sales_amount).toFixed(2);
                var sales_order_no = this.sales_order_no;
                var orderBean_pk = this.orderBean_pk;
                if (!Number.isNaN(Number(sales_amount)) && Number(sales_amount) > 0) {
                    var totalPrice = this.totalPrice;
                    var price = parseInt(this.payPrice) + parseInt(sales_amount);
                    if (price > totalPrice) {
                        this.$alert('该订单总金额为 ' + totalPrice + ' 元，您累计已付款 ' + this.payPrice + ' 元，本次付款金额 ' + sales_amount + ' 元，已超过订单金额，请修改付款金额', '提示', {
                            confirmButtonText: '确定',
                            callback: action => {
                                this.sales_amount = 0;
                            }
                        });
                    } else {
                        this.loading = true;
                        $.ajax({
                            url: '/order/payment.json',
                            type: 'post',
                            data: {
                                path: file,
                                price: sales_amount,
                                orderNo: sales_order_no,
                                orderBean_pk: orderBean_pk
                            },
                            // async: false,
                            dataType: 'json',
                            success: (result) => {
                                this.loading = false;
                                if (result.ret) {
                                    this.$message({
                                        message: "付款成功",
                                        type: 'success'
                                    });
                                    this.sales_order_no = '';
                                    // this.search();
                                    this.dialogFormVisible = false;
                                    window.location.reload();
                                } else {
                                    this.$message({
                                        message: result.msg,
                                        type: 'error'
                                    });
                                }
                            }
                        });
                    }
                } else {
                    this.$message.error('请输入合法的金额');
                }

            },
            //状态转换
            formatStatus: function (row, column) {
                return row.orderBill_customStat == 1000 ? '已保存（待供方确认）' : row.orderBill_customStat ==
                1001 ? '下单中（待确认）' : row.orderBill_customStat ==
                1002 ? '付款中(已付款' + row.payment + ")" : row.orderBill_customStat == 1003 ? '待收货' : row.orderBill_customStat == 1004 ? '已完成' : '未知';
            },
            search() {
                this.currentPage=1;
                this.pageSize=100;
                this.ajaxFunction();
            },

            //修改用户
            modify(sales_order_no, price, orderBean_pk) {
                this.loading = true;
                this.payPrice = 0;
                this.totalPrice = price;
                this.sales_order_no = sales_order_no;
                this.orderBean_pk = orderBean_pk;
                var resultData = {};
                $.ajax({
                    url: '/order/gathering.json',
                    type: 'post',
                    data: {orderNo: sales_order_no, orderBean_pk: orderBean_pk},
                    // async: false,
                    dataType: 'json',
                    success: (result) => {
                        this.loading = false;
                        if (result.ret) {
                            resultData = result.data;
                            var arr = resultData.list;
                            var payPrice = this.payPrice;
                            for (var i = 0; i < arr.length; i++) {
                                payPrice += parseInt(arr[i].payAmout);
                            }
                            this.payPrice = payPrice;
                        } else {
                            this.$message.error('获取累计付款金额异常');
                        }
                    }
                });
                this.dialogFormVisible = true

            },
            //确认订单
            affirmOrder(val, type, code, orderBean_pk) {
                var msg = "确定执行该操作吗?";
                if (type == 'affirm') {
                    msg = '确定订单后，将无法修改，是否确认?'
                } else if (type == 'del') {
                    msg = '此操作将会彻底删除订单，是否继续?'
                }
                this.$confirm(msg, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.operateOrder(val, type, code, orderBean_pk);
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消操作'
                    });
                });
            },
            //查看详情
            detail(val, orderBean_pk) {
                window.location.href = "/order/orderDetail.page?orderNo=" + val + "&orderBean_pk=" + orderBean_pk;
            },
            async handleSizeChange(val) {
                this.pageSize = val;
                this.ajaxFunction();
            },
            //
            async handleCurrentChange(val) {
                this.currentPage = val;
                this.ajaxFunction();

            },

            operateOrder(val, type, code, orderBean_pk) {
                this.loading = true;
                $.ajax({
                    url: '/order/operate.json',
                    type: 'post',
                    data: {orderNo: val, type: type, code: code, orderBean_pk: orderBean_pk, admin: "false"},
                    // async: false,
                    dataType: 'json',
                    success: (result) => {
                        this.loading = false;
                        if (result.ret) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                            this.ajaxFunction();
                        } else {
                            this.$message({
                                message: result.msg,
                                type: 'error'
                            });
                        }
                    }
                });
            },
            ajaxFunction() {
                this.tableLoading = true;
                //声明要发送的参数
                var orderNo = this.sales_order_no;
                var userName = this.sales_man;
                var beginTime = this.startTime;
                var endTime = this.endTime;
                var state = this.sales_status;
                var page = this.currentPage;
                var size = this.pageSize;
                var buyer = this.buyer;
                var resultData = '';
                $.ajax({
                    url: '/order/orderList.json',
                    type: 'post',
                    data: {
                        orderNo: orderNo,
                        userName: userName,
                        startTime: beginTime,
                        endTime: endTime,
                        sales_status: state,
                        buyer: buyer,
                        page: page,
                        size: size
                    },
                    // async: false,
                    dataType: 'json',
                    success: (result) => {
                        this.tableLoading = false;
                        if (result.ret) {
                            resultData = result.data;
                            this.tableData = resultData.list;
                            this.totalRecords = resultData.total;
                        } else {
                            this.$message({
                                message: "列表加载失败，请稍后重试。",
                                type: 'error'
                            });
                        }
                    }
                });
            },
        },

        async mounted() {
            this.ajaxFunction();
        }
    })
</script>
</body>

</html>